package com.example.calculation;



import com.example.entity.Chef;
import com.example.entity.Effect;
import com.example.entity.OfficialGameData;
import com.example.entity.Skill;
import com.example.utils.JSON;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * 创建好GlobalAddtion对象后 ，因该直接将六维属性赋值好，
 *
 * */


public class GlobalAddtion implements Serializable{
    //男厨师
    //烤技法
    public int bake;
    //煮技法
    public int boil;
    //炒技法
    public int stirfry;
    //切技法
    public int knife;
    //炸技法
    public int fry;
    //蒸技法
    public int steam;
    //男厨师技法加成
    public int manfill;
    //女厨师技法加成
    public int womanfill;
    //厨具售价加成
    public double price;

    //各个等级菜谱售价加成
    public double[] useall={0,0,0,0,0,0};
    //菜谱上限加成
    public int[] maxequiplimit={0,0,0,0,0,0};

    public GlobalAddtion(OfficialGameData officialGameData ){
        init(officialGameData);
    }

    public GlobalAddtion(Chef[] chefs, List<Skill> skills1 ){
        init(chefs,skills1);
    }


    private void init( Chef[] chefs,List<Skill> skills1 ) {
        Map<Integer, Skill> skills = new HashMap<Integer, Skill>();
        for (Skill skill : skills1) {
            skills.put(skill.getSkillId(), skill);
        }
        for (Chef chef : chefs) {
            //如果修炼了
            if (hasXiuLian(chef)) {//因为一技能都是对自身起作用， 所以只看修炼技能
                Skill skill = skills.get(chef.getUltimateSkill());
                if (skill == null) {continue;}
                for (Effect effect : skill.getEffect()) {
                    parseEffect(effect);
                }
            }
            Skill skill = skills.get(chef.getSkill());
            if (skill == null) {continue;}
            for (Effect effect : skill.getEffect()) {
                parseEffect(effect);
            }

        }

        for (int i = 0; i < useall.length; i++) {
            useall[i]=useall[i]/100;
        }

        //由于省事情， 所以 manfill和womanfill的值 被重复计算了6次 ，因此需要除6

        manfill/=6;
        womanfill/=6;
    }


    private void parseEffect(Effect effect){
        final String type = effect.getType();
        if (effect.getCondition().equals("Global")) {
            if (effect.getTag() != null) {
                if (effect.getTag() == 1) { //男厨师加成
                    manfill = manfill + effect.getValue();
                } else if (effect.getTag() == 2) { //女厨师加成
                    womanfill = womanfill + effect.getValue();
                }
            } else {
                switch (type) {
                    case "Bake":
                        bake = bake + effect.getValue();
                        break;
                    case "Steam":
                        steam = steam + effect.getValue();
                        break;
                    case "Boil":
                        boil =boil + effect.getValue();
                        break;
                    case "Fry":
                        fry = fry + effect.getValue();
                        break;
                    case "Knife":
                        knife = knife + effect.getValue();
                        break;
                    case "Stirfry":
                        stirfry = stirfry + effect.getValue();
                        break;
                    case "MaxEquipLimit":  //菜谱上限
                        maxequiplimit[effect.getRarity()]+= effect.getValue();
                        break;
                    case "UseAll":    //菜谱售价
                        useall[effect.getRarity()]+= effect.getValue() ;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void init(OfficialGameData officialGameData ) {
        //全部节能列表
        List<Skill> skills1 = officialGameData.getSkills();
        //计算修炼对各个技法的加成
        List<Chef> chefs = officialGameData.getChefs();


        init(chefs.toArray(new Chef[0]),skills1);
    }

    private boolean hasXiuLian(Chef chef){
        if (chef.getUltimateSkill()==null){
            return false;
        }
        return true;
    }

    public int getBake() {
        return bake;
    }

    public void setBake(int bake) {
        this.bake = bake;
    }

    public int getBoil() {
        return boil;
    }

    public void setBoil(int boil) {
        this.boil = boil;
    }

    public int getStirfry() {
        return stirfry;
    }

    public void setStirfry(int stirfry) {
        this.stirfry = stirfry;
    }

    public int getKnife() {
        return knife;
    }

    public void setKnife(int knife) {
        this.knife = knife;
    }

    public int getFry() {
        return fry;
    }

    public void setFry(int fry) {
        this.fry = fry;
    }

    public int getSteam() {
        return steam;
    }

    public void setSteam(int steam) {
        this.steam = steam;
    }

    public int getManfill() {
        return manfill;
    }

    public void setManfill(int manfill) {
        this.manfill = manfill;
    }

    public int getWomanfill() {
        return womanfill;
    }

    public void setWomanfill(int womanfill) {
        this.womanfill = womanfill;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double[] getUseall() {
        return useall;
    }

    public void setUseall(double[] useall) {
        this.useall = useall;
    }

    public int[] getMaxequiplimit() {
        return maxequiplimit;
    }

    public void setMaxequiplimit(int[] maxequiplimit) {
        this.maxequiplimit = maxequiplimit;
    }

    @Override
    public String toString() {
                try {
            return JSON.getObjectMapper().writeValueAsString(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
